// pages/love/love.js

const request = require("../../utils/request.js");
const util = require('../../utils/util.js');
const app = getApp();
let loadMoreEnable = true;
let refreshEnable = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        windowWidth: "",
        list: [],
        grant_type: "client_credential",
        appid: "wx390cf67fc95b5eb3",
        secret: "d43ee765b3a3341df967fb59688a88e7",
        offset: 0,
        count: 4,
        hideLoading: false,
        hasMore: true,
        noContent: false,
        marks: 0
    },

    detailGo: function (event) {
        let that = this;
        let index = event.currentTarget.dataset.id;
        let url = that.data.list[index].content.news_item[0].url;
        let params = {
            url: that.data.list[index].content.news_item[0].url
        };


        wx.navigateTo({
            url: '../article/article?url=' + encodeURIComponent(url),
            success: function (res) {
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },

    getList: function (params, type) {
        let that = this;
        if (type == 1) {
            if (refreshEnable) {
                refreshEnable = false;
                loadMoreEnable = false;
                request.request('m/weixin/accesstoken.json', params).then(function (res) {
                

                    console.log('111',that.data.page,res);
                    refreshEnable = true;
                    loadMoreEnable = true;
                    let len = 0, listIndex = 0;
                    let oneday = 1000 * 60 * 60 * 24;
                    let now = new Date().getTime();
                    let nowDate = util.formatDate(new Date());

                    let getTime = nowDate.year + '/' + nowDate.month + '/' + nowDate.day

                    let yesterday = new Date(getTime).getTime() / 1000;

                    for (let j = 0; j < res.data.result.item.length; j++) {
                        let update_time = res.data.result.item[j].content.update_time;
                        //console.log(update_time, yesterday, yesterday - update_time, len, getTime);
                        if (update_time < yesterday) {
                            let param = {};
                            let val = "list[" + (len + listIndex) + ']';
                            param[val] = res.data.result.item[j];
                            listIndex++;
                            that.setData(param);
                        }

                    }


                    that.setData({
                        // list: res.data.result.item,
                        hideLoading: true,
                        offset: ++that.data.offset,
                    });
                    if (that.data.marks < that.data.offset) {
                      that.setData({
                        marks: that.data.offset
                      })
                    }
                    wx.hideNavigationBarLoading();
                });
            }

        } else if (type == 2) {
            if (loadMoreEnable) {
                loadMoreEnable = false;
                request.request('m/weixin/accesstoken.json', params).then(function (res) {
                    loadMoreEnable = true;
                    //console.log('222', that.data.page, res);
                    if (res.data.result.item.length == 0) {
                        that.setData({
                            hasMore: false,
                            noContent: true
                        });
                        setTimeout(function () {
                            that.setData({
                                noContent: false
                            })
                        }, 5000);
                        return;
                    }


                    let len = that.data.list.length;
                    for (let j = 0; j < res.data.result.item.length; j++) {
                        let param = {};
                        let val = "list[" + (len + j) + ']';
                        param[val] = res.data.result.item[j];
                        that.setData(param);
                    }

                    that.setData({
                        hideLoading: true,
                        hasMore: false,
                        offset: ++that.data.offset
                       
                    });
                    if(that.data.marks < that.data.offset) {
                      that.setData({
                        marks: that.data.offset
                      })
                    } else {
                      that.setData({
                        offset: ++that.data.marks
                      })
                    }
                });
            }

        }
    },
    loadMore: function () {
        this.setData({
            hasMore: true
        });
        let params = {
            grant_type: this.data.grant_type,
            appid: this.data.appid,
            offset: this.data.marks * this.data.count,
            count: this.data.count
          };
        
      
        //console.log('aaa', this.data.marks);
        this.getList(params, 2);
    },

    refresh: function () {
        wx.showNavigationBarLoading();
        this.setData({
            offset: 0,
            hasMore: false,
            noContent: false
        });
        let params = {
            grant_type: this.data.grant_type,
            appid: this.data.appid,
            offset: this.data.offset,
            count: this.data.count
        };
        this.getList(params, 1);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        let params = {
            grant_type: that.data.grant_type,
            appid: that.data.appid,
            offset: that.data.offset,
            count: that.data.count
        };
        this.getList(params, 1);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

